package com.course.android.ui.fragment;

import android.app.Dialog;
import android.view.View;

import androidx.annotation.MainThread;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.baselib.android.cach.PreferceKey;
import com.baselib.android.cach.PreferceManager;
import com.baselib.android.event.EventManager;
import com.baselib.android.utils.ToastTool;
import com.course.android.R;
import com.course.android.activity.ActivityIntent;
import com.course.android.ui.adapter.VideoNewsAdapter;
import com.course.android.ui.adapter.WebNewsAdapter;
import com.course.controller.NewsController;
import com.course.controller.UserController;
import com.course.controller.permission.PermissionController;
import com.course.controller.permission.PermissionManager;
import com.course.controller.permission.PermissionReqCode;
import com.course.model.HttpConfigBean;
import com.course.utils.web.HttpAction;
import com.course.utils.web.HttpRespCallBack;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class VideoFragment extends BaseNewsNetFragment {

    FloatingActionButton fab;
    VideoNewsAdapter webNewsAdapter;
    @Override
    protected void initView(View view) {
        EventManager.getInstance().register(this);
        initLv(view);
        webNewsAdapter = new VideoNewsAdapter();
        lvView.setAdapter(webNewsAdapter);  // 设置循环视图的线性适配器
        fab = view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UserController.getInstance().isLogin(getContext())){
                    Dialog dialog = PermissionController.getManager().getPersissionDialog(getActivity(), PermissionReqCode.REQ_VIDEO);
                    if (dialog!=null){
                        dialog.show();
                        return;
                    }
                    ActivityIntent.toVideoIntent(getActivity());
                    return;
                }
                ActivityIntent.toLoginsIntent(getActivity());
            }
        });
    }


//eventBus
    @Subscribe(threadMode  = ThreadMode.MAIN)
    public void msgFromEvent(List<String> msg) {
        // do sth
        if (msg==null||msg.size()!=2){
            return;
        }
        String video=null;
        String img=null;
        for (String item:msg){
            if (item.endsWith("jpg")
                    ||item.toLowerCase().endsWith("jepg")
                    ||item.toLowerCase().endsWith("png")){
                img = item;
            }else {
                video = item;
            }
        }
        NewsController.getInstance().videoUpload(this,video,img);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventManager.getInstance().unregister(this);
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
        loadNext();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video;
    }

    @Override
    protected void lazyLoad() {

    }
    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), RecyclerView.VERTICAL, false);
        return manager;
    }

    @Override
    public void loadNext() {
        NewsController.getInstance().query(this,"sch","video",pageNum,pageSize);
    }

    @Override
    public <T> void onData(HttpConfigBean flag, T o) {
//        loadNext();
        if (flag.getActionIndex() == HttpAction.newVideoUpload.getValue()){
            loadNext();
        }


    }

    @Override
    public <T> void onPageData(HttpConfigBean flag, int pageNum, int pageSize, int total, List<T> array) {
        webNewsAdapter.setData((List<com.course.model.resp.NewsVo>) array);
    }
}
